<template>
    <div class="type-nav">
        <div class="container" @mouseenter="doShowMiniNav(false)" @mouseleave="doShowMiniNav(true)">
            <h2 class="all" data-true="1">全部商品分类</h2>
            <nav class="nav">
                <a href="###">服装城</a>
                <a href="###">美妆馆</a>
                <a href="###">尚品汇超市</a>
                <a href="###">全球购</a>
                <a href="###">闪购</a>
                <a href="###">团购</a>
                <a href="###">有趣</a>
                <a href="###">秒杀</a>
            </nav>
            <transition
                name="animate__animated animate__bounce animate__faster"
                enter-active-class="animate__fadeIn"
                leave-active-class="animate__fadeOut"
            >
                <div class="sort" v-show="!show" data-true="1">
                    <div class="all-sort-list2">
                        <div
                            class="item"
                            v-for="datas, index in ListData"
                            :key="datas.categoryId"
                            v-show="index < 16"
                            @mouseenter="displayBoxShow($event, true)"
                            :data-listNum="index"
                            @mouseleave="displayBoxShow($event, false)"
                        >
                            <h3>
                                <a
                                    href="###"
                                    @click.prevent="getSerchKey(`${datas.categoryId}:${datas.categoryName}`, 'category1Id')"
                                >{{ datas.categoryName }}</a>
                            </h3>
                            <div
                                class="item-list clearfix"
                                :style="{ 'display': index == showList ? 'block' : 'none' }"
                            >
                                <div class="subitem">
                                    <dl
                                        class="fore"
                                        v-for="doubleData, index in datas.categoryChild"
                                        :key="doubleData.categoryId"
                                        v-show="index < 8"
                                    >
                                        <dt>
                                            <a
                                                href="###"
                                                @click.prevent="getSerchKey(`${doubleData.categoryId}:${doubleData.categoryName}`, 'category2Id')"
                                            >{{ doubleData.categoryName }}</a>
                                        </dt>
                                        <dd>
                                            <em v-for="threeData in  doubleData.categoryChild">
                                                <a
                                                    href="###"
                                                    @click.prevent="getSerchKey(`${threeData.categoryId}:${threeData.categoryName}`, 'category3Id')"
                                                >{{ threeData.categoryName }}</a>
                                            </em>
                                        </dd>
                                    </dl>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </transition>
        </div>
    </div>
</template>

<script lang='ts'>
import 'animate.css'
import TypeSum from '@/TypeSum/entry'
import { useRouter, useRoute } from 'vue-router'
import { ref, defineComponent, inject, onMounted, reactive, toRefs, nextTick } from 'vue'
import bus from '@/EventBus/EventBus'
type Data = TypeSum['Nav']['NavData']
export default defineComponent({
    name: 'Nav',
    props: ['showMiniNav'],
    setup() {
        let router = useRouter()
        let route = useRoute()
        let Datas = reactive<Data>({
            showList: -1
        })
        let ListData = inject<Array<any>>('InterlockListData')
        let show = ref<boolean>(true), miniShow = ref<boolean>(false)
        function getSerchKey(key: string, floor: "category1Id" | "category2Id" | "category3Id") {
            nextTick(() => bus.emit('setAttrBread'))
            router.push({
                name: 'search',
                query: {
                    ...route.query,
                    [floor]: key
                }
            })
        }
        function doShowMiniNav(value: boolean) {
            if (miniShow.value) {
                show.value = value
            } else {
                show.value = false
            }
        }
        function displayBoxShow(event: MouseEvent, model: boolean) {
            if (event.target instanceof HTMLElement && model) {
                Datas.showList = parseInt(event.target.dataset.listnum as string)
            } else {
                Datas.showList = -1
            }
        }
        onMounted(() => {
            bus.on('showDisplay', (value) => {
                if (typeof value == 'boolean') {
                    miniShow.value = value
                    doShowMiniNav(value)
                }
            })
        })
        return {
            ListData, doShowMiniNav, show, getSerchKey, displayBoxShow, ...toRefs(Datas)
        }
    }
})
</script>
<style scoped lang="less">
.type-nav {
    border-bottom: 2px solid #e1251b;

    .container {
        width: 1200px;
        margin: 0 auto;
        display: flex;
        position: relative;

        .all {
            width: 210px;
            height: 45px;
            background-color: #e1251b;
            line-height: 45px;
            text-align: center;
            color: #fff;
            font-size: 14px;
            font-weight: bold;
        }

        .nav {
            a {
                height: 45px;
                margin: 0 22px;
                line-height: 45px;
                font-size: 16px;
                color: #333;
            }
        }

        .sort {
            position: absolute;
            left: 0;
            top: 45px;
            width: 210px;
            height: 481px;
            position: absolute;
            background: #fafafa;
            z-index: 999;

            .all-sort-list2 {
                .item {
                    h3 {
                        line-height: 30px;
                        font-size: 14px;
                        font-weight: 400;
                        overflow: hidden;
                        padding: 0 20px;
                        margin: 0;

                        a {
                            color: #333;
                        }
                    }

                    .item-list {
                        display: none;
                        position: absolute;
                        width: 734px;
                        min-height: 480px;
                        background: #f7f7f7;
                        left: 210px;
                        border: 1px solid #ddd;
                        top: 0;
                        z-index: 9999 !important;

                        .subitem {
                            float: left;
                            width: 650px;
                            padding: 0 4px 0 8px;

                            dl {
                                border-top: 1px solid #eee;
                                padding: 6px 0;
                                overflow: hidden;
                                zoom: 1;

                                &.fore {
                                    border-top: 0;
                                }

                                dt {
                                    float: left;
                                    width: 54px;
                                    line-height: 22px;
                                    text-align: right;
                                    padding: 3px 6px 0 0;
                                    font-weight: 700;
                                }

                                dd {
                                    float: left;
                                    width: 415px;
                                    padding: 3px 0 0;
                                    overflow: hidden;

                                    em {
                                        float: left;
                                        height: 14px;
                                        line-height: 14px;
                                        padding: 0 8px;
                                        margin-top: 5px;
                                        border-left: 1px solid #ccc;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>